<?php
Yii::import('application.jpgraph.*');
require_once('jpgraph.php');
require_once('jpgraph_line.php');

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	/**
	 * 最近30天评论数的折线图
	 */
	public function actionJpgraph_30d()
	{
		$model = new Site();
		$data = $model->getEstimateLineNum('1');
		$ydata = array();
		$date_array = array();
		$today = date("Y-m-d");
		for($i = 19; $i >=0; $i--){
			array_push($date_array, date("m-d",strtotime("$today -$i day")));
		}
		
		foreach($date_array as $value){
			if(array_key_exists($value,$data)){
				$ydata[] = $data[$value];
			}else{
				$ydata[] = 0;
			}
		}
		$this->showGraph($date_array, $ydata, '最近30天的评论数统计');
	}
	
	/*
	 * 最近6个月的评论数折线图  
	 * */
	public function actionJpgraph_6m()
	{
		$model = new Site();
		$data = $model->getEstimateLineNum('2');
		$ydata = array();
		$date_array = array();
		$today = date("Y-m-d");
		for($i = 5; $i >=0; $i--){
			array_push($date_array, date("Y-m",strtotime("$today -$i month")));
		}
		if($data)
		{
			foreach($date_array as $value){
				if(array_key_exists($value,$data)){
					$ydata[] = $data[$value];
				}else{
					$ydata[] = 0;
				}
			}
		}
		$this->showGraph($date_array, $ydata, '最近6个月的评论数统计');
	}
	
	/**
	 * 显示图表
	 *   */
	protected function showGraph($date_array, $ydata, $title)
	{
		// Size of the overall graph
		$width=900;
		$height=300;
		
		// Create the graph and set a scale.
		// These two calls are always required
		$graph = new Graph($width,$height);
		$graph->SetScale('intlin');
		$graph->SetShadow();
		
		// Setup margin and titles
		$graph->SetMargin(40,40,40,60);
		$title = iconv("utf-8", "gb2312", $title);//jpgraph对中文默认是gb2312 所以要先转码至gb2312
		$graph->title->Set($title);
		$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
		$graph->SetMarginColor('#84C1FF');
		
		$graph->yaxis->title->SetFont( FF_FONT1 , FS_BOLD );
		$graph->xaxis->title->SetFont( FF_FONT1 , FS_BOLD );
		$graph->xaxis->SetTickLabels($date_array);
		$graph->xaxis->SetTextLabelInterval(1);
		
		
		//$graph->yaxis->SetColor('blue');
		
		// Create the linear plot
		$lineplot=new LinePlot($ydata);
		$lineplot->SetColor( 'blue' );
		$lineplot->SetWeight( 2 );   // Two pixel wide
		$lineplot->value->SetFont(FF_ARIAL,FS_BOLD);
		$lineplot->value->SetColor('red');
		$lineplot->value->SetFormatCallback('separator1000');
		$lineplot->mark->SetType(MARK_UTRIANGLE);
		$lineplot->mark->SetColor('blue');
		$lineplot->mark->SetFillColor('red');
		
		function separator1000($aVal) {
			return number_format($aVal);
		}
		
		$lineplot->value->Show();
		
		// Add the plot to the graph
		$graph->Add($lineplot);
		
		// Display the graph
		$graph->Stroke();
	}
}